package com.demo.netty.tomcat;

import com.demo.tomcat.MyRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MyNettyRequest {

    private ChannelHandlerContext cxt;
    private HttpRequest httpRequest;

    public MyNettyRequest(ChannelHandlerContext cxt, HttpRequest httpRequest) {

        this.cxt = cxt;
        this.httpRequest = httpRequest;
    }


    public String getMethod() {
        return httpRequest.getMethod().name();
    }


    public String getUrl() {
     return httpRequest.getUri();
    }

    public Map<String, List<String>> getParameters() {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.getUri());
        return queryStringDecoder.parameters();
    }

    public String getParameter(String name) {
        Map<String, List<String>> parameters = getParameters();
        List<String> strings = parameters.get(name);
        if (Objects.isNull(strings)) {
            return null;
        }else {
            return strings.get(0);
        }
    }
}
